/*
 * CadastroRevistaAction.java
 *
 * Created on 22 de Julho de 2007, 12:03
 */

package br.usp.poli.mfc.struts.action;

import br.usp.poli.mfc.struts.DAO.DAOFactory;
import br.usp.poli.mfc.struts.DAO.RevistaDAO;
import br.usp.poli.mfc.struts.actionform.CadastroRevistaActionForm;
import br.usp.poli.mfc.struts.bean.Revista;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
/**
 *
 * @author Renato
 * @version
 */

public class CadastroRevistaAction extends Action {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String ERROR = "error";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        CadastroRevistaActionForm frmCadRev = (CadastroRevistaActionForm) form;
        Revista _revista = new Revista();
        if(frmCadRev == null) System.out.println("tah null");
        System.out.println("data" + frmCadRev.getDatapublicacao());
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date myDate = dateFormat.parse(frmCadRev.getDatapublicacao());
        _revista.setTitulo(frmCadRev.getTitulo());
        _revista.setDescricao(frmCadRev.getDescricao());
        _revista.setIDGenero(Integer.parseInt(frmCadRev.getGenero()));
        _revista.setDatapublicacao(myDate);
        RevistaDAO _revistaDAO = DAOFactory.getRevistaDAO();
        if(_revistaDAO.InsereRevista(_revista))
            return mapping.findForward(ERROR);
        else
            return mapping.findForward(SUCCESS);

    }
}
